package me.hunche.app.web.page;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.hunche.app.domain.Activity;
import me.hunche.app.domain.ActivityMember;
import me.hunche.app.service.ActivityMemberService;
import me.hunche.app.service.ActivityService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.feinno.framework.common.web.AbstractJQueryEntityController;

@Controller
@RequestMapping(value = "/members")
public class ActivityMemberManagerController extends AbstractJQueryEntityController<ActivityMember, ActivityMemberService> {

	private static Map<Integer, String> allTypes = new LinkedHashMap();
	static {
		allTypes.put(1, "队长");
		allTypes.put(2, "正式队员");
		allTypes.put(3, "后备队员");
	}

	@SuppressWarnings("unused")
	@Autowired
	private ActivityMemberService memberService;
	
	@Autowired
	private ActivityService activityService;

	
	@Override
	protected void referenceData(HttpServletRequest request, Map<String, Object> model) {
		model.put("allTypes", allTypes);
	}
	
	private DateFormat df = new SimpleDateFormat("HH:mm");

	@RequestMapping(value="{memberId}/gone")
	public String gone(@PathVariable(value="memberId") Long memberId,Model model){
		ActivityMember m = memberService.get(memberId);
		memberService.gone(m);
		Activity a = activityService.findActivityById(m.getActivityId());
		a.getMembers();
		
		model.addAttribute("self" , m.getNickname());
		
		item:a.getMembers().add(a.getLeader());
		
		List<Map> list = new ArrayList();
		for(ActivityMember item:a.getMembers()){
			Map map = new HashMap();
			map.put("nickname", item.getNickname());
			map.put("mobile", item.getMobile());
			if(item.hasGone()){
				map.put("time", df.format(item.getGoneTime()));
			}
			list.add(map);
		}
		
		model.addAttribute("members", list);
		return "member_gone.jsp";
	}
	
	@RequestMapping(value="{memberId}/ready")
	public String ready(@PathVariable(value="memberId") Long memberId){
		ActivityMember m = memberService.get(memberId);
		memberService.ready(m);
		return "member_ready";
	}

}
